package com.example.web.interceptor;

import com.sso.client.service.SsoWebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : alex
 * @date : 2020/1/22
 */
@Service
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    SsoWebClient ssoWebClient;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 验证token
        if (ssoWebClient.checkToken(request, response)) {
            return true;
        }
        // 验证不通过重定向到sso登录页面
        ssoWebClient.redirectLogin(request, response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
